<?php

/* * ************************************************************************* 
 * item_special_model.class.php
 * Encoding : UTF-8
 * Desc. :商品信息副表数据库实现类
 * Created on : 2013-4-23 10:45:07 by Aiden , aiden.ke@doosnet.net
 * Updated on : 2013-4-23 10:45:07 by Aiden , aiden.ke@doosnet.net
 * ************************************************************************* */

defined('IN_SPIDER') or exit('No permission resources.');

pc_base::load_sys_class('model', '', 0);

class item_special_model extends model {

    public function __construct() {
        $this->db_config = pc_base::load_config('database');
        $this->db_setting = 'default';
        $this->table_name = '';
        parent::__construct();
    }

    /**
     * 设置商品数据表
     * @param string $tablename 商城名称
     */
    public function table_name($mallname) {
        $this->table_name = $this->db_config[$this->db_setting]['tablepre'] . $mallname;
        return $this->table_name;
    }

    /**
     * 更新下架记录
     * @param selId $int 商城标识ID
     * @param currentTime $int 当前时间
     * @param item_no $string 下架ID集合
     */
    public function update_shelfviews($selId, $currentTime, $item_no) {
        if ($item_no) {
            $table_name = pc_base::load_config('tablemap', $selId);
            //设置时间
            $currentTime = $currentTime ? $currentTime : getmicrotime(true);

            //执行Sql语句
            $sql = "update `{$table_name}_special` set `last_update`='{$currentTime}',`shelfviews`=shelfviews+1,`stock`=0 WHERE `mall_id` = {$selId} AND `item_no` in ($item_no); ";
            $this->query($sql);

            if (!pc_base::load_config('system', 'debug')) {//不调试时启用
                echo $sql . '<br/>';
            }
        }
    }

    /**
     * 更新主表价格记录
     * @param tableName $string 表名
     * @param mall_id $int 商城标识ID
     * @param item_no $int 商品ID
     * @param price $int 商品价格
     * @param score $int 商品评分
     * @param reviews $int 商品销量
     * @param trendprice $int 价格趋势
     * @param stock $int 商品库存
     */
    public function update_price($tableName, $mall_id, $item_no, $price, $score='0', $reviews='0', $trendprice='0', $stock='1') {
        $currentTime = getmicrotime(TRUE);

        $field = '';
        if (is_numeric($reviews) && $reviews > 0) {
            $field.="`reviews`='{$reviews}',";
            if (is_numeric($score) && $score > 0) {
                $field.="`score`='{$score}',";
            }
        }

        if ($price != 0) {
            $sql = " update `{$tableName}` set `goods_price` ='{$price}',{$field} `last_update`='{$currentTime}',`shelfviews`=0,`stock`='{$stock}',`trend`={$trendprice} where `mall_id`={$mall_id} AND `item_no`='{$item_no}';";
            $this->query($sql);
        } else {
            if ($mall_id == 6) {
                if (in_array($item_no, array('2306113', '26853', '3052183', '3698703', '709983', '706773', '19843'))) {
                    $stock = 0;
                }
                if ($stock == 0) {//亚马逊 缺货时，获取不了价格，需要特殊处理
                    $sql = " update `{$tableName}` set {$field} `last_update`='{$currentTime}',`shelfviews`=0,`stock`='{$stock}',`trend`={$trendprice} where `mall_id`={$mall_id} AND `item_no`='{$item_no}';";
                    $this->query($sql);
                }
            }
        }

        if (!pc_base::load_config('system', 'debug')) {//不调试时启用
            echo $sql . '<br/><br/>';
        }
    }

}

?>